﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KMFrame.Permission.Common;
using KMFrame.Permission.NBL.DBEntities;
using KMFrame.Permission.NBL;
using NBearLite;

namespace KMFrame.Permission.BLL
{
    public class Roles : PermissionLogic
    {
        public class RoleInfo
        {
            public int RoleID { get; set; }
            public string RoleName { get; set; }
            public string RoleDescription { get; set; }
        }

        public RoleInfo GetRoleInfo(int RoleID)
        {
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                DBColumns.KM_Roles.RoleID, 
                DBColumns.KM_Roles.RoleName,
                DBColumns.KM_Roles.RoleDescription
            };
            return NBLData
                .Select(DBColumns.KM_Roles, selectColumns)
                .Where(DBColumns.KM_Roles.RoleID == RoleID)
                .ToSingleObject<RoleInfo>();
        }

        public bool UpdateRoleInfo(RoleInfo Info)
        {
            bool result = false;
            UpdateSqlSection update = NBLData.Update(DBColumns.KM_Roles)
                .AddColumn(DBColumns.KM_Roles.RoleName, Info.RoleName)
                .AddColumn(DBColumns.KM_Roles.RoleDescription, Info.RoleDescription);
            int ri = update
                .Where(DBColumns.KM_Roles.RoleID == Info.RoleID)
                .Execute();
            if (ri == 1)
                result = true;
            return result;
        }

        public IEnumerable<KM_Roles> GetRolesList()
        {
            IEnumerable<KM_Roles> list = NBLData.Select(DBColumns.KM_Roles).ToList<KM_Roles>();
            return list;
        }

        public IEnumerable<RoleInfo> GetRoleInfoList()
        {
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                DBColumns.KM_Roles.RoleID, 
                DBColumns.KM_Roles.RoleName,
                DBColumns.KM_Roles.RoleDescription
            };
            IEnumerable<RoleInfo> list = NBLData.Select(DBColumns.KM_Roles, selectColumns)
                .ToList<RoleInfo>();

            return list;
        }

        /// <summary>
        /// 读取用户角色列表
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public IEnumerable<RoleInfo> GetUserRoleList(int UserID)
        {
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                DBColumns.KM_Roles.RoleID, 
                DBColumns.KM_Roles.RoleName,
                DBColumns.KM_Roles.RoleDescription
            };
            IEnumerable<RoleInfo> list = NBLData.Select(DBColumns.KM_UserRoles, selectColumns)
                .Join(DBColumns.KM_Roles, DBColumns.KM_UserRoles.RoleID == DBColumns.KM_Roles.RoleID)
                .Where(DBColumns.KM_UserRoles.UserID == UserID)
                .ToList<RoleInfo>();

            return list;
        }

        public string GetUserRoles(int UserID)
        {
            StringBuilder sb = new StringBuilder();
            IEnumerable<RoleInfo> roles = GetUserRoleList(UserID);
            foreach (RoleInfo r in roles)
            {
                sb.Append(r.RoleName);
                sb.Append(",");
            }
            return sb.ToString();
        }
    }
}
